package com.bbs.crawl;

import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.bean.BBSOfPCAuto;
import com.bbs.dao.BBSDaoOfPCAuto;
import com.car.util.DocUtilProxy;
import com.car.util.IPUtil;
import com.car.util.Util;

public class BBSCrawlOfPCAuto {
	private static Logger logger = Logger.getLogger(BBSCrawlOfPCAuto.class.getName());
	private BBSDaoOfPCAuto bbsDaoOfPCAuto;
	private int bbsid;
	private DocUtilProxy DocUtil;
	private List<String> ipList;
	private int index;
	int j = 0;

	public BBSCrawlOfPCAuto(BBSDaoOfPCAuto bbsDaoOfPCAuto, int bbsid, DocUtilProxy docUtil, List<String> ipList,
			int index) {
		super();
		this.bbsDaoOfPCAuto = bbsDaoOfPCAuto;
		this.bbsid = bbsid;
		DocUtil = docUtil;
		this.ipList = ipList;
		this.index = index;
	}

	public void run() {
		this.startCrawl(this.bbsid);
	}

	// 得到帖子页数
	public int getPages(int bbsid) {
		int page = 1;
		String url = "http://bbs.pcauto.com.cn/forum-" + bbsid + ".html";
		Document doc = DocUtil.getDocProxy(url, ipList, index);
		System.out.println(url);
		if (doc == null) {
			return 0;
		}
		try {
			Elements pages = doc.select("div.pager a");
			int size = pages.size();
			Element p = pages.get(size - 2);
			page = Integer.parseInt(Util.getMatched(p.text(), "\\d{1,}").group(0));
		} catch (Exception e) {
		}
		return page;
	}

	public void startCrawl(int bbsid) {
		int page = getPages(bbsid);
		if (page == 0) {
			return;
		}
		bbsDaoOfPCAuto.createTable(bbsid);
		logger.info(bbsid + "开始");
		for (int i = 1; i < page + 1; i++) {
			String url = "http://bbs.pcauto.com.cn/forum-" + bbsid + "-" + i + ".html";
			Document doc = DocUtil.getDocProxy(url, ipList, index);
			if (doc == null) {
				return;
			}
			Elements forum = doc.select("th.title span.checkbox_title a");
			for (Element e : forum) {
				String bbsUrl = e.attr("href");
				parseBBS(bbsUrl, bbsid);
			}
		}
		bbsDaoOfPCAuto.updateBBSTime("pcauto_bbsgxsj", bbsid);
		logger.info(bbsid + "更新完成" + "  " + j);

	}

	public void parseBBS(String url, int bbs_ppid) {
		Document doc = DocUtil.getDocProxy(url, ipList, index);
		try {
			int bbs_id = Integer.parseInt(url.split("-")[1].split("\\.")[0]);
			String bbs_bt = doc.select("i#subjectTitle").get(0).text();
			int bbs_sfjh = doc.select("div.pick1").size() > 0 ? 1 : 0;
			int bbs_sfwd = doc.select("a.ask").size() > 0 ? 1 : 0;
			String bbs_fbsj = Util.getMatched(doc.select("div.post_time").text(), "\\d{4}-\\d{2}-\\d{2}").group(0);
			String bbs_nr = doc.select("div.post_msg").select("div.replyBody").get(0).text();
			int bbs_hfs = Integer.parseInt(doc.select("span.yh").get(1).text());
			Element userInfo = doc.select("div.user_meg_con").get(0);
			Element userName = userInfo.select("div.avatar a").get(0);
			int bbs_yhid = Integer.parseInt(userName.attr("href").split("/")[3]);
			String bbs_yhmc = userName.select("img").get(0).attr("alt");
			Element u0 = userInfo.select("dl dd.devote_num").get(0);
			String bbs_zc = u0.text();
			Element u1 = userInfo.select("dl dd.devote_num").get(1);
			String bbs_tx = u1.select("a").text();
			Element u2 = userInfo.select("dl dd.devote_num").get(2);
			int bbs_jf = Integer.parseInt(u2.select("a").text());
			Elements is = userInfo.select("div.user_meg_link i em");
			int bbs_zt = Integer.parseInt(is.get(0).text());
			int bbs_ht = Integer.parseInt(is.get(1).text());
			int bbs_jh = Integer.parseInt(is.get(2).text());
			BBSOfPCAuto bbs = new BBSOfPCAuto(bbs_id, bbs_ppid, bbs_bt, bbs_sfjh, bbs_sfwd, bbs_fbsj, bbs_nr, bbs_hfs,
					bbs_yhid, bbs_yhmc, bbs_zc, bbs_tx, bbs_jf, bbs_zt, bbs_ht, bbs_jh);
			bbsDaoOfPCAuto.insertBBS(bbs);
			j++;
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) {
		BBSDaoOfPCAuto bbsDaoOfPCAuto = new BBSDaoOfPCAuto();
		DocUtilProxy DocUtil = new DocUtilProxy();
		IPUtil i = new IPUtil();
		List<String> ipList = i.getIP();
		BBSCrawlOfPCAuto bbs = new BBSCrawlOfPCAuto(bbsDaoOfPCAuto, 2734, DocUtil, ipList, 200);

		bbs.run();
	}
}
